package com.gpframework.module.controller.manufacture;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.manufacture.domain.GpProductionScheduling;
import com.gpframework.module.manufacture.domain.req.GpProductionSchedulingAdd;
import com.gpframework.module.manufacture.domain.req.GpProductionSchedulingReq;
import com.gpframework.module.manufacture.domain.req.GpProductionSchedulingUpdate;
import com.gpframework.module.manufacture.domain.req.SelectProgressReq;
import com.gpframework.module.manufacture.domain.vo.GpProductionSchedulingVo;
import com.gpframework.module.manufacture.service.IGpProductionSchedulingService;
import com.gpframework.module.module.service.IGpDeliveryNoteService;
import com.gpframework.module.process.domain.req.GpProcessSchedulingUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpProductionSchedulingController
 * @Author gpframework
 * @Date 2023-12-12
 * @Version 1.0.0
 * @Description 生产排单Controller
 */
@Validated
@Api(tags = "生产排单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/manufacture/productionScheduling" )
public class GpProductionSchedulingController extends BaseController {

    private final IGpProductionSchedulingService iGpProductionSchedulingService;
    private final IGpDeliveryNoteService iGpDeliveryNoteService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-12
  * @Param gpProductionScheduling
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:list')")
    @GetMapping("/list")
    public TableDataInfo<GpProductionSchedulingVo> list(GpProductionSchedulingReq gpProductionSchedulingReq)
    {
        startPage();
        QueryWrapper<GpProductionScheduling> lqw = new QueryWrapper<GpProductionScheduling>();
        gpProductionSchedulingReq.generatorQuery(lqw,true);
        List<GpProductionSchedulingVo> list = iGpProductionSchedulingService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:add')" )
    @Log(title = "添加生产排单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpProductionSchedulingAdd gpProductionSchedulingAdd) {
        GpProductionScheduling gpProductionScheduling = Convert.convert(new TypeReference<GpProductionScheduling>() {}, gpProductionSchedulingAdd);
        return toAjax(iGpProductionSchedulingService.save(gpProductionScheduling) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:edit')" )
    @Log(title = "修改生产排单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpProductionSchedulingUpdate> list) {

        return toAjax(iGpProductionSchedulingService.edit(list) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:export')" )
    @Log(title = "导出生产排单" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpProductionSchedulingReq gpProductionSchedulingReq) {
        QueryWrapper<GpProductionScheduling> lqw = new QueryWrapper<GpProductionScheduling>();
        gpProductionSchedulingReq.generatorQuery(lqw,true);
        List<GpProductionSchedulingVo> list = iGpProductionSchedulingService.listVo(lqw);
        ExcelUtil<GpProductionSchedulingVo> util = new ExcelUtil<GpProductionSchedulingVo>(GpProductionSchedulingVo. class);
        return util.exportExcel(list, "productionScheduling" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpProductionSchedulingVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpProductionSchedulingService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:remove')" )
    @Log(title = "删除生产排单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpProductionSchedulingService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    /**
     * @Description 生成生产到货单
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("生成生产到货单")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:add')" )
    @Log(title = "生成生产到货单" , businessType = BusinessType.INSERT)
    @PostMapping("/createDeliveryNote")
    public AjaxResult createDeliveryNote(@Valid @RequestBody GpProductionSchedulingUpdate gpProductionDetailsUpdate) {
        if (gpProductionDetailsUpdate == null || CollectionUtils.isEmpty(gpProductionDetailsUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpDeliveryNoteService.createDeliveryNote(gpProductionDetailsUpdate.getIds(), null) ? 1 : 0);
    }

    @ApiOperation("获取环思智慧进度")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:list')")
    @GetMapping("/selectProgress")
    public AjaxResult<String> selectProgress(SelectProgressReq selectProgressReq)
    {
        String body = HttpRequest.post("http://hsapp.huansi.org/BIShow.aspx?ParaStr=q%2bO90Q%2bRp9NhmRsgJcLfPA%3d%3d&Language=cn")
                .form("__VIEWSTATE", "/wEPDwUKMTg0NDY4MzI5M2RkRr4cFiGNvEYceIzs7eIUBox7hL6erQowSqb1q72ksPE=")
                .form("__VIEWSTATEGENERATOR", "4D860858")
                .form("dOrderDateBeg", selectProgressReq.getDOrderDateBeg())
                .form("dOrderDateEnd", selectProgressReq.getDOrderDateEnd())
                .form("sMaterialLot", selectProgressReq.getSMaterialLot())
                .execute().body();
        return AjaxResult.success(body);
    }

    /**
     * @Description 更新环思智慧进度
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("更新环思智慧进度")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:edit')" )
    @Log(title = "更新环思智慧进度" , businessType = BusinessType.UPDATE)
    @PutMapping("/updateProgress")
    public AjaxResult updateProgress(@Valid @RequestBody GpProductionSchedulingUpdate gpProductionSchedulingUpdate) {
        if (gpProductionSchedulingUpdate == null || gpProductionSchedulingUpdate.getId() == null || StringUtils.isBlank(gpProductionSchedulingUpdate.getProgressStatus())) {
            return AjaxResult.parameterError();
        }
        UpdateWrapper<GpProductionScheduling> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("progress_status", gpProductionSchedulingUpdate.getProgressStatus());
        updateWrapper.eq("id", gpProductionSchedulingUpdate.getId());
        return toAjax(iGpProductionSchedulingService.update(updateWrapper) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('manufacture:productionScheduling:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpProductionSchedulingUpdate gpProductionDetailsUpdate) {
        if (gpProductionDetailsUpdate == null || gpProductionDetailsUpdate.getId() == null || gpProductionDetailsUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpProductionDetailsUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpProductionSchedulingService.copy(gpProductionDetailsUpdate.getId(), gpProductionDetailsUpdate.getCopyNum()) ? 1 : 0);
    }



    /**
     * @Description 拆分订单
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("拆分订单")
    @PreAuthorize("@ss.hasPermi('process:splitOrders:add')" )
    @Log(title = "拆分订单" , businessType = BusinessType.INSERT)
    @PostMapping("/splitOrders")
    public AjaxResult splitOrders(@Valid @RequestBody GpProductionSchedulingUpdate gpProductionDetailsUpdate) {
        if (gpProductionDetailsUpdate == null || gpProductionDetailsUpdate.getId() == null || gpProductionDetailsUpdate.getSplitNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpProductionDetailsUpdate.getSplitNum() < 1) {
            return AjaxResult.error("拆分的胚布库存剩余数量不能小于1");
        }
        return toAjax(iGpProductionSchedulingService.splitOrders(gpProductionDetailsUpdate.getId(), gpProductionDetailsUpdate.getSplitNum()) ? 1 : 0);
    }
}
